package 代码记录.指定公司代码练习记录.华为_2024.分值200;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * @author zx
 * @create 2024-02-16 14:51
 * 人家的思路很棒，debug一遍很清楚；二刷要能做出来
 */
public class 贪心歌手_57 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        int N = sc.nextInt();
        int costDays = 0;
        for (int i = 0; i < N + 1; i++) {
            int days = sc.nextInt();
            costDays += days;
        }
        int haveDays = T - costDays;
        List<City> cities = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            int M = sc.nextInt();
            int D = sc.nextInt();
            cities.add(new City(M, D));
        }
        int income = 0;
        //遍历剩余天数的次数
        for (int i = 0; i < haveDays; i++) {
            //对城市列表根据收入降序排序，然后每一天从收入最高的城市获取收入
            Collections.sort(cities);
            if (!cities.isEmpty() && cities.get(0).income > 0) {
                income += cities.get(0).income;
                cities.get(0).income -= cities.get(0).decrease;
            }
        }
        System.out.println(income);
    }
    static class City implements Comparable<City> {
        int income;
        int decrease;

        public City(int income, int decrease) {
            this.income = income;
            this.decrease = decrease;
        }

        @Override
        public int compareTo(City other) {
            return Integer.compare(other.income, this.income);
        }
    }
}
